package com.yzcrm.erp.admin.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.yzcrm.erp.admin.entity.AdminExamine;
import com.yzcrm.erp.admin.entity.AdminExamineRecord;
import com.yzcrm.erp.admin.service.AdminExamineRecordService;
import com.yzcrm.erp.crm.common.CrmEnum;
import com.yzcrm.swagger.annotation.ActionApi;
import com.yzcrm.swagger.annotation.Api;
import com.yzcrm.swagger.annotation.Param;
import com.yzcrm.swagger.annotation.Params;
import com.yzcrm.utils.AuthUtil;
import com.yzcrm.utils.R;

/**
 * 审核合同或回款
 * @author zxy
 */
@Api(remark = "审核")
public class AdminExamineRecordController extends Controller {
    @Inject
    private AdminExamineRecordService examineRecordService;

    /**
     * 审核合同或者回款 recordId:审核记录id status:审批状态：审核状态  1 审核通过 2 审核拒绝 4 已撤回
     * remarks:审核备注 id:审核对象的id（合同或者回款的id）
     */
    public void auditExamine(){
        Integer recordId = getInt("recordId");
        Integer status = getInt("status");
        Integer id = getInt("id");
        String remarks = get("remarks");
        String batchId = get("batchId");
        Long nextUserId = getLong("nextUserId");
        Long ownerUserId = getLong("ownerUserId");
        Long newOwnerId = getLong("newOwnerId");
        Integer customerType = getInt("customerType");
        // 采购审核 倒数第二次审核可动态增加审核人
        Long checkUserId = getLong("checkUserId");

        // 出库时最终审核时需要设置快递信息及产品序列号  {"data":"{entity:null, products:null}"}
        String data = get("data");
        JSONObject jsonObject = null;
        if(ObjectUtil.isNotNull(data)) {
            jsonObject = JSON.parseObject(data);
        }
//        renderJson(R.ok());
        renderJson(examineRecordService.auditExamine(recordId,status,remarks,batchId,id,nextUserId,ownerUserId,newOwnerId, jsonObject, checkUserId,customerType));
    }

    @ActionApi(summary = "判断是否是倒数第二个审核", remark="判断是否是倒数第二个审核")
    @Params({
            @Param(name="recordId", remark="当前数据审批记录", dataType="int", defaultValue="", required=true),
            @Param(name="label", remark="", dataType="int", defaultValue="", required=true)
    })
    public void nextToLastExamineUser() {

        Integer recordId = getInt("recordId");
        Integer label = getInt("label");
        renderJson(examineRecordService.nextToLastExamineUser(recordId, label));
    }
    /**
     * 根据审核记录id，查询审核日志
     * recordId 审核记录id
     */
    public void queryExamineLogList(){
        Integer recordId = getInt("recordId");
        //暂时取消权限校验
//        if(getExamineObjIdByRecordId(recordId)){
//            renderJson(R.noAuth()); return;
//        }
        renderJson(examineRecordService.queryExamineLogList(recordId));
    }
    /**
     * 根据审核记录id，查询审核日志
     * recordId 审核记录id ownerUserId 负责人ID
     */
    public void queryExamineRecordList(){
        Integer recordId = getInt("recordId");
        Long ownerUserId = getLong("ownerUserId");
        //暂时取消权限校验
//        if(getExamineObjIdByRecordId(recordId)){
//            renderJson(R.noAuth()); return;
//        }
        renderJson(examineRecordService.queryExamineRecordList(recordId,ownerUserId));
    }

    /**
     * 根据recordId查询权限
     * @param recordId
     * @return
     */
    private boolean getExamineObjIdByRecordId(Integer recordId){
        boolean auth = false;
        
        AdminExamineRecord examineRecord = AdminExamineRecord.dao.findById(recordId);
        AdminExamine examine = AdminExamine.dao.findById(examineRecord.getExamineId());
        Integer id = null;
        switch(examine.getCategoryType()) {
	        case 1: 
	        	//合同
	        	id = Db.queryInt("select contract_id from `yzcrm_crm_contract` where examine_record_id = ?",recordId);
	        	auth = AuthUtil.isCrmAuth(CrmEnum.CRM_CONTRACT,id);
	        	break;
	        case 2:
	        	//回款
	        	id = Db.queryInt("select receivables_id from `yzcrm_crm_receivables` where examine_record_id = ?",recordId);
	            auth = AuthUtil.isCrmAuth(CrmEnum.CRM_RECEIVABLES,id);
	        	break;
	        case 3:
	        	//线索
	        	id = Db.queryInt("select leads_id from `yzcrm_crm_leads` where examine_record_id = ?",recordId);
	        	auth = AuthUtil.isCrmAuth(CrmEnum.CRM_LEADS,id);
	        	break;
	        case 4:
	        	//客户
	        	id = Db.queryInt("select customer_id from `yzcrm_crm_customer` where examine_record_id = ?",recordId);
	        	auth = AuthUtil.isCrmAuth(CrmEnum.CRM_CUSTOMER,id);
	        	break;
	        case 5:
	        	//商机
	        	id = Db.queryInt("select business_id from `yzcrm_crm_business` where examine_record_id = ?",recordId);
	        	auth = AuthUtil.isCrmAuth(CrmEnum.CRM_BUSINESS,id);
	        	break;
	        case 11:
	        	//申请售前
	        	id = Db.queryInt("select pre_sales_id from `yzcrm_crm_preSales` where examine_record_id = ?",recordId);
	        	auth = AuthUtil.isCrmAuth(CrmEnum.CRM_PRE_SALES,id);
	        	break;
        }
        return auth;
    }
}
